//
//  MTMyFoucsVC.m
//  met
//
//  Created by 李晓俊 on 2019/5/11.
//  Copyright © 2019 李晓俊. All rights reserved.
//

#import "MTMyFoucsVC.h"
#import "MTFoucsCell.h"
#import "MTCancleView.h"
#import "MTNewUserInfoVC.h"

#import "ChatViewController.h"

@interface MTMyFoucsVC ()
@property (nonatomic,strong) NSMutableArray *dataArr;

@property (nonatomic, strong) UIView *bgView;

@end

@implementation MTMyFoucsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableFooterView = [UIView new];
//    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//        self.page++;
//        [self loadData];
//    }];
    self.dataArr = [NSMutableArray new];
    [self getInfo2];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self getInfo];
}
-(void)loadData{
    if (self.tableView.mj_footer ) {
        [self.tableView.mj_footer endRefreshing];
    }
    [self getInfo];
}
- (void)getInfo {
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AccountService *acc = [AccountService getObject];
    acc.httpRequestResultBlock = ^(id data,int code){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (code == 1) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
                self.dataArr = [NSMutableArray arrayWithArray:data];
                if (self.removeFocus) {
                    self.removeFocus([NSString stringWithFormat:@"%lu",(unsigned long)self.dataArr.count]);
                }
            }else {
                NSArray *info = data;
                if (info.count == 0) {
                    self.page --;
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else if (info.count < 10) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }
                for (FoucsModel *model in info) {
                    [self.dataArr  addObject:model];
                }
            }
            [self.tableView reloadData];
            //            if (self.dataArr.count == 0) {
            //                [self createViewBynoMsgName:@"暂无动态" alertMsg:@""];
            //                [self.view addSubview:self.noMsgView];
            //                self.noMsgView.hidden = NO;
            //            }else {
            //                self.noMsgView.hidden = YES;
            //            }
        }else {
            [self.view maketishiToast:data];
        }
    };
    [acc findFriends:[NSString stringWithFormat:@"%ld",(long)self.page]];
}
- (void)getInfo2 {
    AccountService *acc = [AccountService getObject];
    acc.httpRequestResultBlock = ^(id data,int code){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (code == 1) {
            NSArray *ar = [NSMutableArray arrayWithArray:data];
            if (self.removeFocus) {
                self.removeFocus2([NSString stringWithFormat:@"%lu",(unsigned long)ar.count]);
            }
        }else {
        }
    };
    [acc findFriendsFans:[NSString stringWithFormat:@"%ld",(long)self.page]];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MTFoucsCell *cell = [MTFoucsCell cellWithTableView:tableView];
    FoucsModel *model = [self.dataArr objectAtIndex:indexPath.row];
    cell.model = model;
    
    cell.addBtnClick = ^(NSString *  sid) {
        //取消关注
        [self createbgView:sid];
    };
    
    return cell;
}

- (void)createbgView:(NSString *)sid {
    self.bgView = [UIView new];
    [[UIApplication sharedApplication].keyWindow addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    UIView *bg = [[UIView alloc]init];
    bg.backgroundColor = [UIColor blackColor];
    bg.alpha = 0.6;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removebgView)];
    [bg addGestureRecognizer:tap];
    [self.bgView addSubview:bg];
    [bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    MTCancleView *cancleView = [[MTCancleView alloc]initWithFrame:CGRectMake(10, kScreenHeight/2.0-77, kScreenWidth-20, 154)];
    cancleView.cancle = ^(NSString * s) {
        [self removebgView];
    };
    cancleView.done = ^(NSString * s) {
        [self removebgView];
//        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        AccountService *acc = [AccountService getObject];
        acc.httpRequestResultBlock = ^(id data,int code){
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if (code == 1) {
                [[UIApplication sharedApplication].keyWindow maketishiToast:@"取消成功"];
                [self getInfo];
            }else {
                [[UIApplication sharedApplication].keyWindow maketishiToast:data];
            }
        };
        [acc friendsUpdate:sid];
    };
    [self.bgView addSubview:cancleView];
}
- (void)removebgView {
    [self.bgView removeFromSuperview];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 75;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 10.0f;
    }
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *v = [UIView new];
    return v;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [UIView new];
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return;
    }
    FoucsModel *model = [self.dataArr objectAtIndex:indexPath.row];
//    MTUserInfoVC *vc = [[MTUserInfoVC alloc] init];
//    vc.userId = [NSString stringWithFormat:@"%@",model.usersId];
//    vc.isMutual = [NSString stringWithFormat:@"%@",model.isMutual];
//    vc.createType = @"1";
////    [NSString stringWithFormat:@"%@",model.createType];
//    vc.nickname = [NSString stringWithFormat:@"%@",model.nickName];
    
//    MTNewUserInfoVC *vc = [[MTNewUserInfoVC alloc] init];
//    vc.userId = [NSString stringWithFormat:@"%@",model.usersId];
//    vc.isMutual = [NSString stringWithFormat:@"%@",model.isMutual];
//    vc.createType = @"1";
//    vc.nickname = [NSString stringWithFormat:@"%@",model.nickName];
//    [self.navigationController pushViewController:vc animated:YES];
    
    
    Message *msg =  [[Message alloc] init];
    msg.userId =[NSString stringWithFormat:@"met_%@",model.usersId];
    msg.username = [NSString stringWithFormat:@"%@", model.nickName];
    msg.headImageUrl = [NSString stringWithFormat:@"%@",model.portraitMap];
    [Message insertObject:msg];
    
    ChatViewController *chatController = [[ChatViewController alloc] initWithConversationChatter:[NSString stringWithFormat:@"met_%@",model.usersId] conversationType:EMConversationTypeChat];
    chatController.title =  model.nickName;
    chatController.username =  model.nickName;
    chatController.userid =  [NSString stringWithFormat:@"%@",model.usersId];

    [self.navigationController pushViewController:chatController animated:YES];
    
//    [self.navigationController pushViewController:vc animated:YES];
}


//补全tableview的线
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{\
    cell.separatorInset = UIEdgeInsetsZero;\
    cell.layoutMargins = UIEdgeInsetsZero; \
    cell.preservesSuperviewLayoutMargins = NO; \
}

@end
